﻿using System;

namespace Infrastructure.Extensions
{
    /// <summary>
    /// 时间戳
    /// </summary>
    public static class TimeStampExtenisons
    {
        /// <summary>
        /// UTC时间戳(秒)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUtcSecondeTimeStamp(this DateTimeOffset dt)
        {
            DateTimeOffset origin = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
            return (long)(dt - origin).TotalSeconds;
        }

        /// <summary>
        /// UTC时间戳(毫秒)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUtcMillisecondsTimeStamp(this DateTimeOffset dt)
        {
            DateTimeOffset origin = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
            return (long)(dt - origin).TotalMilliseconds;
        }

        /// <summary>
        /// 时间戳转本地时间(ticks)
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTimeOffset ToLocalDateTimeOffsetTicks(long timeStamp)
        {
            return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeStamp).ToLocalTime();
        }

        /// <summary>
        /// 时间戳转本地时间(秒)
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTimeOffset ToLocalDateTimeOffsetSeconds(long timeStamp)
        {
            return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddSeconds(timeStamp).ToLocalTime();
        }

        /// <summary>
        /// 时间戳转本地时间(毫秒)
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTimeOffset ToLocalDateTimeOffsetMilliseconds(long timeStamp)
        {
            return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddMilliseconds(timeStamp).ToLocalTime();
        }

        /// <summary>
        /// 时间戳转本地时间(秒)
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime UtcToLocalDateTimeSeconds(long timeStamp)
        {
            return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddSeconds(timeStamp);
        }

        /// <summary>
        /// 时间戳转本地时间(毫秒)
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime UtcToLocalDateTimeMilliseconds(long timeStamp)
        {
            return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds(timeStamp);
        }

        /// <summary>
        /// 年月日时分秒转时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long ToTimeStamp(this DateTime dateTime)
        {
            return long.Parse(dateTime.ToString("yyyyMMddHHmmss"));
        }

        /// <summary>
        /// 年月日时分秒时间戳转本地时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(long timeStamp)
        {
            return DateTime.Parse(timeStamp.ToString());
        }
    }
}
